﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMark.DAO
{
    public class SlideShowModels
    {
        public static List<SLIDESHOW> GetAll()
        {
            using (var context = new EMartEntities())
            {
                return context.SLIDESHOWs.ToList();
            }
        }
        public static SLIDESHOW GetByID(long ID)
        {
            using (var context = new EMartEntities())
            {
                return context.SLIDESHOWs.Find(ID);
            }
        }
        public static bool Add(SLIDESHOW slideShow)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    context.SLIDESHOWs.Add(slideShow);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Update(SLIDESHOW slideShow)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SLIDESHOW existing = context.SLIDESHOWs.Find(slideShow.ID);
                    existing.HinhAnh = slideShow.HinhAnh;
                    existing.Link = slideShow.Link;
                    existing.MoTa = slideShow.MoTa;
                    existing.ThuTu = slideShow.ThuTu;
                    existing.TieuDe = slideShow.TieuDe;
                    existing.TrangThai = slideShow.TrangThai;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool UpdateStatus(long ID, int Status)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SLIDESHOW existing = context.SLIDESHOWs.Find(ID);                    
                    existing.TrangThai = 1 - Status;                    
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Delete(long ID)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SLIDESHOW slideShow = context.SLIDESHOWs.Find(ID);
                    context.SLIDESHOWs.Remove(slideShow);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static int GetCurrentOrdinal()
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    return context.SLIDESHOWs.Max(u => u.ThuTu);
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}